package video.dali.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import video.dali.common.PageResult;
import video.dali.common.dto.UserVideo;
import video.dali.common.dto.VideoDto;
import video.dali.common.dto.VideosCategoryPageDto;
import video.dali.common.entity.Video;
import video.dali.common.vo.VideoVo;
import video.dali.mapper.VideoMapper;
import video.dali.service.VideoService;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    /**
     * 上传文件
     * @param videoDto
     */
    @Override
    public void upload(VideoDto videoDto) throws IOException {
        List<MultipartFile> files = videoDto.getFiles();
        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
            String tansFerTo = "E:\\dager\\" + originalFilename;
            List<Video> videoList = videoMapper.getByUserId(videoDto.getUserId());
            for (Video video : videoList) {
                if (video.getName().equals(originalFilename)) {
                    return;
                }
            }
            Video video = new Video();
            BeanUtils.copyProperties(videoDto, video);
            video.setCreateTime(LocalDateTime.now());
            video.setAddress(tansFerTo);
            if (originalFilename != null) {
                video.setName(originalFilename.substring(0, originalFilename.lastIndexOf(".")));
            }
            videoMapper.insert(video);
            file.transferTo(new File(tansFerTo));
        }

    }


    /**
     * 删除视频
     * @param userVideo
     */
    @Override
    public void deleteUserVideo(UserVideo userVideo) throws IOException {
        videoMapper.deleteByIdAndUserId(userVideo);
    }

    /**
     * 根据userId查询视频
     * @param
     * @return
     */
    @Override
    public List<Video> listByUserId() {
        return videoMapper.selectAll();
    }


    /**
     * 根据id查询视频
     * @param id
     * @return
     */
    @Override
    public VideoVo getById(Integer id) {
        Video video = videoMapper.getById(id);
        VideoVo videoVo = new VideoVo();
        BeanUtils.copyProperties(video, videoVo);
        return videoVo;
    }


    /**
     * 根据分类id查询视频
     * @param videosCategoryPageDto
     * @return
     */
    @Override
    public PageResult getVideosByCategoryId(VideosCategoryPageDto videosCategoryPageDto) {
        PageHelper.startPage(videosCategoryPageDto.getPage(),videosCategoryPageDto.getPageSize());
        Page<Video> page = videoMapper.getVideosByCategoryId(videosCategoryPageDto.getCategoryId());
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }


}
